<?php
FLEA::loadClass('Controller_Public');
class Controller_Default extends Controller_Public
{
	var $tableAlbums;
	var $tableStyles;
	var $style;

	function Controller_Default(){
		$this->tableAlbums = FLEA::getSingleton('Model_Albums');

		$this->tableStyles = FLEA::getSingleton('Model_Styles');
		$this->style = $this->tableStyles->findAll(null,'style_id ASC');
	}
	
	function actionIndex(){	

		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

		FLEA::loadClass('Controller_Pager');
		//$condition = 'album_id DESC';
		$pager = new Controller_Pager($this->tableAlbums, $page-1,FLEA::getAppInf('pageSize'),$condition,'update_date DESC');
		$tableStyles = FLEA::getSingleton('Model_Styles');
		$style = $tableStyles->findAll(null,'style_id ASC');

		
		$smarty = &$this->_getView();
		$smarty->assign('url', FLEA::getAppInf('webAddr'));
		$smarty->assign('styleArray', $this->style);
		$smarty->assign('styleId', 0);
		$smarty->assign('albumArray', $pager->findAll());
		$smarty->assign('albumRand', $this->tableAlbums->findAll(null,' rand()',10));
        $smarty->assign('page_info',$pager->getNavBar($this->_url(),null ));
		$smarty->display('default_index.html');
	}

	function actionStyle(){

		if (isset($_GET['styleid'])) {
            $styleId = (int)$_GET['styleid'];
        } else {
            redirect(url());
        }

		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		
		$condition = array('style.style_id'=> $styleId);
		FLEA::loadClass('Controller_Pager');		
		$pager = new Controller_Pager($this->tableAlbums, $page-1,FLEA::getAppInf('pageSize'),$condition,'update_date DESC');

		$smarty = &$this->_getView();
		$smarty->assign('url', FLEA::getAppInf('webAddr'));
		
		$smarty->assign('styleArray', $this->style);
		$smarty->assign('styleId', $styleId);
		$smarty->assign('albumArray', $pager->findAll());
		$smarty->assign('albumRand', $this->tableAlbums->findAll(null,' rand()',10));
		$smarty->assign('page_info',$pager->getNavBar($this->_url(),null) );
		$smarty->display('default_index.html');
		
	}

	function actionView(){
		if (isset($_GET['id'])) {
            $albumId = (int)$_GET['id'];
        } else {
            redirect(url());
        }
		
		$album = $this->tableAlbums->_getAlbum($albumId);
		if(!$album)
			return $this->defaultError('参数错误!');

		$titleName = $album['album_name'].'_'.$album['artists']['artist_name'].'_';
		//相关专辑
		$artistId = $album['artist_id'];
		$condition = array('artist_id'=>$artistId,'album_id'=>array($albumId,'<>'));
		$albums = $this->tableAlbums->findAll($condition);

		$smarty = &$this->_getView();
		$smarty->assign('url', FLEA::getAppInf('webAddr'));
		$smarty->assign('title', $titleName);
		$smarty->assign('styleArray', $this->style);
		$smarty->assign('styleId', -1);
		$smarty->assign('album', $album);
		$smarty->assign('albums', $albums);
		$smarty->assign('albumRand', $this->tableAlbums->findAll(null,' rand()',10));
		$smarty->display('default_album.html');
	}

	function defaultError($msg){
		$smarty = &$this->_getView();
		$smarty->assign('msg', $msg);
		$smarty->assign('styleArray', $this->style);
		$smarty->assign('styleId', -1);
		$smarty->assign('albumRand', $this->tableAlbums->findAll(null,' rand()',16));
		$smarty->display('default_error.html');
	}

	function actionSearch(){	

		$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
		$kwords = $_GET['kwords'];

		FLEA::loadClass('Controller_Pager');
		$condition = array(
						array('album_name',"%$kwords%",'LIKE'),
						array('artists.artist_name',"%$kwords%",'LIKE','OR'),
				);
		$pager = new Controller_Pager($this->tableAlbums, $page-1,FLEA::getAppInf('pageSize'),$condition,'update_date DESC');
		$tableStyles = FLEA::getSingleton('Model_Styles');
		$style = $tableStyles->findAll(null,'style_id ASC');
		
		$smarty = &$this->_getView();
		$smarty->assign('url', FLEA::getAppInf('webAddr'));
		$smarty->assign('styleArray', $this->style);
		$smarty->assign('styleId', -1);
		$smarty->assign('albumArray', $pager->findAll());
		$smarty->assign('albumRand', $this->tableAlbums->findAll(null,' rand()',10));
        $smarty->assign('page_info',$pager->getNavBar($this->_url(),$kwords ));
		$smarty->display('default_index.html');
	}
}
?>